package com.lovesher.payment.service.utils.excel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: DateUtils.java
 * 
 * @Package com.aidai.utils
 * 
 * @Description: TODO(时间操作工具类)
 * 
 * 
 * @version V1.0
 */
public class DateUtils {

	public enum Direction {
		/** 向前推 */
		before,
		/** 向后推 */
		after
	}

	/**
	 * 不可实例化
	 */
	private DateUtils() {
	}

	/**
	 * 获取每月第一天
	 * 
	 * @param date
	 *            月份时间,为空时 取当前时间
	 * @param month
	 *            要减去的月份数, 1表示往前一个月, -1表示往后一个月
	 * @return 时间格式:2015-06-01 00:00:01
	 */
	public static Date getMonthFirstDay(Date date, int month) {
		if (date == null) {
			date = new Date();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) - month, 1, 0, 0, 1);
		return calendar.getTime();
	}

	public static String format(Date date, boolean defail) {
		if (date == null) {
			if (defail) {
				date = new Date();
			} else {
				return "";
			}
		}
		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(date);
	}
	/**
	 * 格式化时间
	 * 
	 * @param date
	 *            时间,为空时 取当前时间
	 * @param format
	 *            格式
	 * @return
	 */
	public static String format(Date date, String format) {
		if (date == null) {
			date = new Date();
		}
		if (format == null) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(date);
	}

	/**
	 * 格式化时间
	 * 
	 * @param date
	 *            时间,为空时 取当前时间
	 * @param format
	 *            格式
	 * @return
	 */
	public static String format(Date date) {
		if (date == null) {
			date = new Date();
		}
		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(date);
	}
	
	/**
	 * 格式化时间
	 * 
	 * @param date
	 *            时间,为空时 返回空
	 * @param format
	 *            格式
	 * @return
	 */
	public static String formatNew(Date date) {
		if (date == null) {
			return "";
		}
		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(date);
	}

	/**
	 * String 字符串转时间类型
	 * 
	 * @param dateStr
	 *            时间字符串
	 * @param format
	 *            格式化
	 * @return
	 */
	public static Date StringToDate(String dateStr, String format) {
		if (format == null) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = simpleDateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static List<String> getMonths(Direction direction, int count) {
		SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy");
		SimpleDateFormat monthFormatter = new SimpleDateFormat("MM");
		int prex = 1;
		if (direction == Direction.before) {
			prex = -1;
		}
		List<String> result = new ArrayList<String>();
		for (int i = 1; i <= count; i++) {
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.MONTH, i * prex);
			Date date = calendar.getTime();
			String resultStr = new StringBuffer(yearFormatter.format(date)).append("/").append(monthFormatter.format(date)).toString();
			result.add(resultStr);
		}
		return result;
	}
	
	public static String dateStr2(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String str = format.format(date);
		return str;
	}
	
	/**
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static Date dateFormat(Date date, String format) {
		String dateStr = format(date, format);
		Date rsltDate = StringToDate(dateStr, format);
		return rsltDate;
	}
	
	/**
	 * 获取N天后的零点
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date getNextNDays(Date date, int num){
		if(null == date){
			date = new Date();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
		cal.add(Calendar.DAY_OF_MONTH, num);
		return cal.getTime();
	}
	
	/**
	 * 
	 * @Title: getAfterDate 
	 * @Description: 获得距指定时间多少天之后的日期 
	 * @param date
	 * @param days
	 * @return Date
	 */
	public static Date getAfterDate(Date date, int days) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + days);
		return now.getTime();
	}

	public static Date rollDate(Date d, int year, int mon, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.YEAR, year);
		cal.add(Calendar.MONTH, mon);
		cal.add(Calendar.DAY_OF_MONTH, day);
		return cal.getTime();
	}


	/**
	 * @param Begin
	 * @param end
	 *            传入开始时间 和 结束时间 格式如：2012-09-07
	 * @return 返回Map 获取相隔多少年 get("YEAR")及为俩个时间年只差，月 天，类推 Key ： YEAR MONTH DAY
	 *         如果开始时间 晚于 结束时间 return null；
	 */

	public static Map<String, Object> getApartTime(String Begin, String end) {
		String[] temp = Begin.split("-");
		String[] temp2 = end.split("-");
		if (temp.length > 1 && temp2.length > 1) {
			Calendar ends = Calendar.getInstance();
			Calendar begin = Calendar.getInstance();

			begin.set(NumberUtils.getInt(temp[0]), NumberUtils.getInt(temp[1]), NumberUtils.getInt(temp[2]));
			ends.set(NumberUtils.getInt(temp2[0]), NumberUtils.getInt(temp2[1]), NumberUtils.getInt(temp2[2]));
			if (begin.compareTo(ends) < 0) {
				Map<String, Object> map = new HashMap<String, Object>();
				ends.add(Calendar.YEAR, -NumberUtils.getInt(temp[0]));
				ends.add(Calendar.MONTH, -NumberUtils.getInt(temp[1]));
				ends.add(Calendar.DATE, -NumberUtils.getInt(temp[2]));
				map.put("YEAR", ends.get(Calendar.YEAR));
				map.put("MONTH", ends.get(Calendar.MONTH) + 1);
				map.put("DAY", ends.get(Calendar.DATE));
				return map;
			}
		}
		return null;
	}
}
